<?php
/**
 *  用户后台中心管理/加载一些统计信息/用户修改个人密码/用户更新个人资料
 *
 * @controller Index
 * @package    Admin
 * @author   zhangYe<banfg56@gmail.com>
 */
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

class IndexController extends BaseController {
	public function _initialize(){
		parent::_initialize();
	}
	
	/**
	 * [index description]
	 * @return [type] [description]
	 */
	public function index(){
		$res = array('rcode'=>0,'msg'=>'','data'=>NULL);
		e_json( $res );
	}

	/**
	 * 前端参数定义
	 * 
	*/
	public function param()
	{
		$param = array();
		//客户池客户状态
		$param['customerStatus'] = array(
			array('status'=>0, 'statusTitle'=> '待审核'),
			array('status'=>1, 'statusTitle'=> '正常'),
			//array('status'=>2, 'statusTitle'=> '禁止登录'),
			array('status'=>3, 'statusTitle'=> '被合并')
		);

		e_json( $param );
	}

	/**
	 * 前端自定义菜单配置
	 * 
	*/
	public function menu()
	{
		$res = array('rcode'=>1,'msg'=>'load menu','data'=>NULL);
		$menuConfig = array(
			'CUSTOMER_VIEW' => array(
							'name' => '客户资源',
							'icon' => 'dist/images/nav_1.png',
							'menus' => array(
									array( 'type'=>'url', 'url' => '/tags', 'name' => '标签管理'),
									array( 'type' => 'div' ),
									array( 'type'=>'url', 'url' => '/customer', 'name' => '客户池')
							)
						),
			'CONTRACT_VIEW' => array(
						'name' => '合同管理',
						'icon' => 'dist/images/nav_2.png',
						'menus' => array(
								array( 'type'=>'url', 'url' => '/contract', 'name' => '客户合同'),
						)
			),
			'MONEY_VIEW' => array(
					'name' => '财务管理',
					'icon' => 'dist/images/nav_3.png',
					'menus' => array(
							array( 'type'=>'url', 'url' => '/caiwu.money', 'name' => '查账流水'),
							array( 'type'=>'url', 'url' => '/caiwu.refund', 'name' => '退款处理'),
					)
			),
			'QYSERVICE_VIEW|PRODUCT_VIEW|PRODUCTLINE_VIEW|PRODUCTPACKAGE_VIEW|SOURCE_VIEW' => array(
					'name' => '营销管理',
					'icon' => 'dist/images/nav_4.png',
					'menus' => array(
							array( 'type'=>'url', 'url' => '/source', 'name' => '渠道管理'),
							array( 'type' => 'div' ),
							array( 'type'=>'url', 'url' => '/productline', 'name' => '产品线管理'),
							array( 'type'=>'url', 'url' => '/product', 'name' => '产品管理'),
							array( 'type'=>'url', 'url' => '/product_package', 'name' => '套餐管理'),
							array( 'type' => 'div' ),
							array( 'type'=>'url', 'url' => '/qyservice', 'name' => '服务管理'),
					)
			),
			'COMPLAIN_VIEW|CONFLICT_VIEW|REFUND_VIEW' => array(
					'name' => '风控合规',
					'icon' => 'dist/images/nav_5.png',
					'menus' => array(
							array( 'type'=>'url', 'url' => '/complain', 'name' => '400记录'),
							array( 'type' => 'div' ),
							array( 'type'=>'url', 'url' => '/conflict', 'name' => '撞单管理'),
							array( 'type'=>'url', 'url' => '/refund', 'name' => '退款处理'),
							array( 'type' => 'div' ),
							array( 'type'=>'url', 'url' => '/risk', 'name' => '风险管理'),
							array( 'type'=>'url', 'url' => '/violations', 'name' => '违规罚款'),
							array( 'type' => 'div' ),
							array( 'type'=>'url', 'url' => '/invoice', 'name' => '发票管理'),
							array( 'type'=>'url', 'url' => '/delivery', 'name' => '邮寄记录')
					)
			),
			'KNOWLEDGE_NODE_ARTICLE|KNOWLEDGE_NODE_MANAGE' => array(
					'name' => '知识库',
					'icon' => 'dist/images/nav_6.png',
					'menus' => array(
							array( 'type'=>'url', 'url' => '/knowledgeproperty', 'name' => '知识点'),
							array( 'type'=>'url', 'url' => '/knowledgeright', 'name' => '节点管理')
					)
			),
			'QY_VIEW|QYAPP_VIEW|ORDERCUSTOMER_CAOZUO' => array(
					'name' => '企业号',
					'icon' => 'dist/images/nav_7.png',
					'menus' => array(
							array( 'type'=>'url', 'url' => '/qy', 'name' => '企业号管理'),
							array( 'type'=>'url', 'url' => '/qyapp', 'name' => '栏目管理'), 
							array( 'type'=>'url', 'url' => '/ordercustomer', 'name' => '订户管理')
					)
			),
		);

		$arrMenu = array();
		$UserService = D('Admin/User','Service');
		$rights = $UserService->getRightById($this->m_user['user_id']);
		if (1==$this->m_user['user_id']){
			$arrMenu = array_values( $menuConfig );
		}
		else if ($rights){
			foreach($menuConfig as $vk=>$vv){
				$tags = explode('|', $vk);
				$find = false;
				foreach($tags as $vt){
					if (isset($rights[$vt])){
						$find = 1;
						break;
					}
				}
				if ($find)
					$arrMenu[] = $vv;
			}
		}
		$res['data'] = array_values($arrMenu);
		e_json( $res );
	}


	/**
	 * 更新个人资料信息
	 * @return array  array('rcode'=>integer,
	 *         				'data'=>客户信息,
	 *             			'msg'=>'操作提示信息')
	 */
	public function updateuser(){
		$res = array('rcode'=>0,'data'=>NULL,'msg'=> L('SERVER_BUSY'));
		$data = array();
		$data['nickname'] = I('post.nickname','','trim');
		$data['email'] = I('post.email','','trim');
		$data['qq'] = I('post.qq','','trim');
		$data['weixin'] = I('post.weixin','','trim');
		$data['phone'] = I('post.phone','','trim');

		$pass = I('post.old_psw',false, 'trim');
		$pass1 = I('post.password1','','trim');
		$pass2 = I('post.password2','','trim');
		$Mdo = D("User");
		$re = false;
		if( $pass1 && $pass1 == $pass2 ){
			$re = $Mdo->updateUser( $this->m_user['user_id'], $data, $pass, $pass1);
		}
		elseif( $pass1 && $pass1 !=$pass2 ){
			$res['msg'] = "新密码前后输入不一致";
		}
		else if( !$pass1 ){
			$re = $Mdo->updateUser( $this->m_user['user_id'], $data, $pass );
		}

		if( $re !== false ){
			$res['rcode'] = $re['r'];
			$res['msg'] = $re['i'];
			$res['data'] = $re['d'];
		}

		e_json( $res );
	}

	/*获取部门、团队业绩*/
	public function sector(){
		$res = array('rcode'=>0,'data'=>NULL,'msg'=>'OK');

		/*获取当前月份的起止时间*/
   		$BeginDate = date('Y-m-01', strtotime(date("Y-m-d")));
   		$EndDate = date('Y-m-d', strtotime( "$BeginDate + 1 month -1 day "));
		$date = array();
		$date[0] = $BeginDate;
		$date[1] = $EndDate;
   		/*获取登录者基本信息*/
		$MTags = M('\Admin\Model\TagsModel:Tag','customer_');
		$uinfo =$MTags->index();
		$user_id = $uinfo['user_id'];
		$sector_id = $uinfo['sector_id'];
		$whereP = array();
		$whereP['sector_id'] = $sector_id;
		$listP = M('sector')->field('parent_sector')->where( $whereP )->select();
		$parent_sector = $listP[0]['parent_sector'];

		/*获取同级部门业绩*/
		$whereT = array();
		$whereT['parent_sector'] = $parent_sector;
		$listT = M('sector') ->field('sector_id,sector_name')->where( $whereT )->select();

		$length_listT = sizeof($listT);
		$where_mountT = array();
		$where_mountT['a.create_time'] = array('between',$date);
		$where_mountT['a.audit_res'] = 1;
		$where_call = array();
		$where_call['endtime'] = array('between',$date);
		$Tdlist = array();
		for($i = 0;$i < $length_listT;$i++) {
			$MSector = M('\Admin\Model\GoalsModel:Sector');
			$arrSector = $MSector->getChild($listT[$i]['sector_id']);
			$whereUser = array();
			$whereUser['sector_id'] = array('IN',$arrSector);
			$arrUser = array();
			$arrUser = M('User')->where($whereUser)->select();
			$lengthUser = sizeof($arrUser);
			if( $lengthUser ){
				$dlist_user = array();
				for($j=0;$j<$lengthUser;$j++){
					$dlist_user[$j] = $arrUser[$j]['user_id'];
				}
				$where_mountT['a.customer_sales'] = array('IN',$dlist_user);
				$dlist = array();
				$dlist = M()->field("sum(a.pay_mount) as mount")
					->table('pay_record as a')
					->join('user as b on b.user_id = a.customer_sales')
					->where($where_mountT)
					->select();
				$Tdlist[$i] = $dlist[0];
			}
			if(!$Tdlist[$i]['mount']){
				$Tdlist[$i]['mount'] = 0;
			}
			$Tdlist[$i]['id'] = $listT[$i]['sector_id'];
			$Tdlist[$i]['name'] = $listT[$i]['sector_name'];
		}
		/*直属(直属)部门业绩*/
		$whereX = array();
		$whereX['parent_sector'] = $sector_id;
		$listX = array();
		$listX = M('sector') ->field('sector_id as id,sector_name as name') ->where( $whereX ) ->select();
		$where_mountX = array();
		$where_mountX['a.create_time'] = array('between',$date);
		$where_mountX['a.audit_res'] = 1;
		$Xdlist = array();
		$Cdlist = array();
		if( $listX ){
			/*有下级部门*/
			$length_listX = sizeof($listX);
			for($i = 0;$i < $length_listX;$i++) {
				$MSector = M('\Admin\Model\GoalsModel:Sector');
				$arrSector = $MSector->getChild($listX[$i]['id']);
				$whereUser = array();
				$whereUser['sector_id'] = array('IN',$arrSector);
				$arrUser = array();
				$arrUser = M('User')->where($whereUser)->select();
				$lengthUser = sizeof($arrUser);
				if( $lengthUser ){
					$dlist_user = array();
					for($j=0;$j<$lengthUser;$j++){
						$dlist_user[$j] = $arrUser[$j]['user_id'];
					}
					$where_mountX['a.customer_sales'] = array('IN',$dlist_user);
					$dlistY = M()->field("sum(a.pay_mount) as mount")
						->table('pay_record as a')
						->join('user as b on b.user_id = a.customer_sales')
						->where($where_mountX)
						->select();

					$Xdlist[$i] = $dlistY[0];


					$where_call['admin_no'] = array('IN',$dlist_user);
					$dlistC = M()->field("sum(a.calltime) as calls")
								->table('calllog as a')
								->join('user as b on b.user_id = a.admin_no')
								->where( $where_call )
								->select();

					$Cdlist[$i] = $dlistC[0];
				}
				if( !$Xdlist[$i]['mount'] ) {
					$Xdlist[$i]['mount'] = 0;
				}
				if( !$Cdlist[$i]['calls'] ) {
					$Cdlist[$i]['calls'] = 0;
				}
				
				$Xdlist[$i]['id'] = $listX[$i]['id'];
				$Xdlist[$i]['name'] = $listX[$i]['name'];

				$Cdlist[$i]['id'] = $listX[$i]['id'];
				$Cdlist[$i]['name'] = $listX[$i]['name'];
			}
		}else{
			/*没有下级部门，求组员(人)*/
			$whereX = array();
			$whereX['sector_id'] = $sector_id;
			$listX = M('user') ->field('user_id as id,nickname as name')->where( $whereX ) ->select();
			$length_listX = sizeof($listX);
			for($i = 0;$i<$length_listX;$i++){
				$where_mountX['a.customer_sales'] = $listX[$i]['id'];
				$dlistY = M()->field("sum(a.pay_mount) as mount")
					->table('pay_record as a')
					->join('user as b on b.user_id = a.customer_sales')
					->where($where_mountX)
					->select();

				$Xdlist[$i] = $dlistY[0];
				$Xdlist[$i]['id'] = $listX[$i]['id'];
				$Xdlist[$i]['name'] = $listX[$i]['name'];
				if( !$Xdlist[$i]['mount'] ) {
					$Xdlist[$i]['mount'] = 0;
				}

				$where_call['admin_no'] = $listX[$i]['id'];
				$dlistC = M()->field("sum(a.calltime) as calls")
							->table('calllog as a')
							->join('user as b on b.user_id = a.admin_no')
							->where( $where_call )
							->select();

				$Cdlist[$i] = $dlistC[0];
				$Cdlist[$i]['id'] = $listX[$i]['id'];
				$Cdlist[$i]['name'] = $listX[$i]['name'];

				if( !$Cdlist[$i]['calls'] ) {
					$Cdlist[$i]['calls'] = 0;
				}
			}
		}

		/*新单业绩*/
		$where_Xd = array();
		$where_Xd['a.customer_sales'] = $user_id;
		$where_Xd['a.create_time'] = array('between',$date);
		$where_Xd['a.pay_type'] = 1;
		$where_Xd['a.audit_res'] = 1;
		$dlist = M()  ->field("sum(a.pay_mount) as mount")
						->table('pay_record as a')
						->join('user as b on b.user_id = a.customer_sales')
						->where( $where_Xd )
						->select();
		$XDlist = $dlist[0];
		if( !$XDlist['mount'] ) {
			$XDlist['mount'] = 0;
		}
		
		/*续费业绩*/
		$where_Xf = array();
		$where_Xf['a.customer_sales'] = $user_id;
		$where_Xf['a.create_time'] = array('between',$date);
		$where_Xf['a.pay_type'] = 2;
		$where_Xf['a.audit_res'] = 1;
		$dlsit = M()  ->field("sum(a.pay_mount) as mount")
						->table('pay_record as a')
						->join('user as b on b.user_id = a.customer_sales')
						->where( $where_Xf )
						->select();
		$XFlist = $dlsit[0];
		if( !$XFlist['mount'] ) {
			$XFlist['mount'] = 0;
		}

		/*客户统计*/
		$where_K = array();
		$where_K['a.start_time'] = array('between',$date);
		$where_K['a.user_id'] = $user_id;

		$dlist = M() ->field("a.customer_xufei_id")
					->table('customer_xufei as a')
					->join('user as b on b.user_id = a.user_id')
					->join('customer as c on c.customer_id = a.customer_id')
					->where( $where_K )
					->count();
		$Kdlist = $dlist;

		if( $Tdlist || $Xdlist || $Cdlist || $XDlist || $XFlist || $Kdlist ){
			$res['rcode'] = 1;
			$res['dataT'] = $Tdlist;
			$res['dataX'] = $Xdlist;
			$res['dataC'] = $Cdlist;
			$res['dataXD'] = $XDlist;
			$res['dataXF'] = $XFlist;
			$res['dataK'] = $Kdlist;
			$res['arrSector'] = $arrUser1;
		}
		e_json($res);
	}

}

/*sum(a.customer_xufei_id) as count*/